/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * AllowedLayersListPanel.java
 *
 * Created on May 25, 2011, 10:23:27 AM
 */
package tasks.ui.architecture;

import common.ControllerException;
import common.DomainException;
import common.Observer;
import common.ObserverInterest;
import domain.DomainFacade;
import domain.DomainFacadeImpl;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author David
 */
public class AllowedLayersListPanel extends javax.swing.JPanel implements Observer {

    /** Creates new form AllowedLayersListPanel */
    public AllowedLayersListPanel() {
        initComponents();
        init();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        allowedLayerList = new javax.swing.JList();
        deleteButton = new javax.swing.JButton();

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 14));
        jLabel1.setText("Allowed To Communicate With");

        allowedLayerList.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        allowedLayerList.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
            public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
                allowedLayerListValueChanged(evt);
            }
        });
        jScrollPane1.setViewportView(allowedLayerList);

        deleteButton.setText("Delete");
        deleteButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                deleteButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 390, Short.MAX_VALUE)
                    .addComponent(deleteButton))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 87, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(deleteButton))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void allowedLayerListValueChanged(javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_allowedLayerListValueChanged
}//GEN-LAST:event_allowedLayerListValueChanged

    private void deleteButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_deleteButtonActionPerformed
        if (controller != null) {
            if (allowedLayerList.getSelectedIndex() != -1) {
                try {
                    controller.removeAllowedLayer(listModel.getLayerAt(allowedLayerList.getSelectedIndex()).toString());
                } catch (DomainException ex) {
                    Logger.getLogger(AllowedLayersListPanel.class.getName()).log(Level.SEVERE, null, ex);
                } catch (ControllerException ex) {
                    Logger.getLogger(AllowedLayersListPanel.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
    }//GEN-LAST:event_deleteButtonActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JList allowedLayerList;
    private javax.swing.JButton deleteButton;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JScrollPane jScrollPane1;
    // End of variables declaration//GEN-END:variables
    private ArchitectureController controller;
    private LayerListModel listModel;
    private DomainFacade facade;

    @Override
    public void notifyChange(ObserverInterest interest) {
        this.setAllowedLayers();
    }

    public void setController(ArchitectureController controller) {
        this.controller = controller;
        this.controller.addObserver(this, ObserverInterest.LAYER_SELECTION_CHANGED);
    }

    private void setAllowedLayers() {
        try {
            listModel = new LayerListModel(this.controller.getAllowedLayersForLayer());
            this.allowedLayerList.setModel(listModel);
            this.allowedLayerList.setEnabled(true);

        } catch (DomainException ex) {
            Logger.getLogger(AllowedLayersListPanel.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ControllerException ex) {
            this.allowedLayerList.setModel(new LayerListModel());
            this.allowedLayerList.setEnabled(false);
        }
    }

    private void init() {
        this.facade = DomainFacadeImpl.getInstance();
        this.facade.addObserver(this, ObserverInterest.LAYERS_CHANGED);
    }
}
